package com.xinli.xinlifm.view.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.HomeBean;
import com.xinli.xinlifm.utils.CircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * created by javac on 16-10-12.
 * email:kylin_javac@outlook.com
 * desc:
 */

public class HomeRecyclerAdapter extends SuperRecyclerAdapter<Object, HomeRecyclerAdapter.ViewHolder> {
    public final Context mContext;

    public HomeRecyclerAdapter(Context context, List<Object> list, int[] layoutId) {
        super(context, list, layoutId);
        mContext = context;

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object o = list.get(position);
        ArrayList o1 = (ArrayList) o;
        if (o1.get(0) instanceof HomeBean.DataBean.TuijianBean) {
            return R.layout.home_viewpager_item;
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.CategoryBean) {
            return R.layout.home_category_iten;
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.HotfmBean) {
            return R.layout.home_hotfm_item;
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.NewfmBean) {
            return R.layout.home_newfm_item;
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.NewlessonBean) {
            return R.layout.home_newfm_item;
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.DiantaiBean) {
            return R.layout.home_diantai_item;
        }
        return 0;
    }

    @Override
    public int getToChild(int position) {
        return 0;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        return new ViewHolder(view,mContext);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Object o = list.get(position);
        ArrayList o1 = (ArrayList) o;
        if (o1.get(0) instanceof HomeBean.DataBean.TuijianBean) {
            if (holder.homeViewpager.getAdapter() == null) {
                ArrayList<HomeBean.DataBean.TuijianBean> tuijianBeen = (ArrayList<HomeBean.DataBean.TuijianBean>) o;
                HomePagerAdapter homePagerAdapter = new HomePagerAdapter(tuijianBeen);
                holder.homeViewpager.setAdapter(homePagerAdapter);
                for (HomeBean.DataBean.TuijianBean bean : tuijianBeen) {
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(R.drawable.point);
                    int v = (int) context.getResources().getDimension(R.dimen.point_padding);
                    imageView.setPadding(v, v, v, v);
                    holder.home_viewpager_lin.addView(imageView);
                }
                holder.homeViewpager.addOnPageChangeListener(holder);
                holder.homeViewpager.setCurrentItem(1);
                holder.home_viewpager_img.post(new Runnable() {
                    @Override
                    public void run() {
                        ViewCompat.setTranslationX(holder.home_viewpager_img, holder.home_viewpager_lin.getLeft());
                    }
                });
                holder.handler.obtainMessage(0, 1, 0).sendToTarget();

            }
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.CategoryBean) {
            ArrayList<HomeBean.DataBean.CategoryBean> categor = (ArrayList<HomeBean.DataBean.CategoryBean>) o;
            Picasso.with(context).load(categor.get(0).getCover()).into(holder.category1);
            Picasso.with(context).load(categor.get(1).getCover()).into(holder.category2);
            Picasso.with(context).load(categor.get(2).getCover()).into(holder.category3);
            Picasso.with(context).load(categor.get(3).getCover()).into(holder.category4);
            Picasso.with(context).load(categor.get(4).getCover()).into(holder.category5);
            Picasso.with(context).load(categor.get(5).getCover()).into(holder.category6);
            Picasso.with(context).load(categor.get(6).getCover()).into(holder.category7);
            Picasso.with(context).load(categor.get(7).getCover()).into(holder.category8);
            holder.categoryText1.setText(categor.get(0).getName());
            holder.categoryText2.setText(categor.get(1).getName());
            holder.categoryText3.setText(categor.get(2).getName());
            holder.categoryText4.setText(categor.get(3).getName());
            holder.categoryText5.setText(categor.get(4).getName());
            holder.categoryText6.setText(categor.get(5).getName());
            holder.categoryText7.setText(categor.get(6).getName());
            holder.categoryText8.setText(categor.get(7).getName());
            holder.category_rl1.setOnClickListener(categor.get(0));
            holder.category_rl2.setOnClickListener(categor.get(1));
            holder.category_rl3.setOnClickListener(categor.get(2));
            holder.category_rl4.setOnClickListener(categor.get(3));
            holder.category_rl5.setOnClickListener(categor.get(4));
            holder.category_rl6.setOnClickListener(categor.get(5));
            holder.category_rl7.setOnClickListener(categor.get(6));
            holder.category_rl8.setOnClickListener(categor.get(7));
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.HotfmBean) {
            ArrayList<HomeBean.DataBean.HotfmBean> hotfmBeen = (ArrayList<HomeBean.DataBean.HotfmBean>) o;
            Picasso.with(context).load(hotfmBeen.get(0).getCover()).into(holder.hotfrmImg1);
            Picasso.with(context).load(hotfmBeen.get(1).getCover()).into(holder.hotfrmImg2);
            Picasso.with(context).load(hotfmBeen.get(2).getCover()).into(holder.hotfrmImg3);
            holder.hotfrmSmalltitle1.setText(hotfmBeen.get(0).getTitle());
            holder.hotfrmSmalltitle2.setText(hotfmBeen.get(1).getTitle());
            holder.hotfrmSmalltitle3.setText(hotfmBeen.get(2).getTitle());
            holder.hotfm_lin1.setOnClickListener(hotfmBeen.get(0));
            holder.hotfm_lin2.setOnClickListener(hotfmBeen.get(1));
            holder.hotfm_lin3.setOnClickListener(hotfmBeen.get(2));
        }else if (o1.get(0) instanceof HomeBean.DataBean.NewlessonBean) {
            ArrayList<HomeBean.DataBean.NewlessonBean> newlessonBeen = (ArrayList<HomeBean.DataBean.NewlessonBean>) o;
            holder.newfmAuthor1.setText(newlessonBeen.get(0).getSpeak());
            holder.newfmAuthor2.setText(newlessonBeen.get(1).getSpeak());
            holder.newfmAuthor3.setText(newlessonBeen.get(2).getSpeak());
            holder.newfmContent1.setText(newlessonBeen.get(0).getTitle());
            holder.newfmContent2.setText(newlessonBeen.get(1).getTitle());
            holder.newfmContent3.setText(newlessonBeen.get(2).getTitle());
            Picasso.with(context).load(newlessonBeen.get(0).getCover()).into(holder.newfmImg1);
            Picasso.with(context).load(newlessonBeen.get(1).getCover()).into(holder.newfmImg2);
            Picasso.with(context).load(newlessonBeen.get(2).getCover()).into(holder.newfmImg3);
            holder.newfrm_title.setText("最新心理课");
            holder.newfm_more.setText("更多心理课");
            holder.home_newfm_rf1.setOnClickListener(newlessonBeen.get(0));
            holder.newfm_rl1.setOnClickListener(newlessonBeen.get(0));
            holder.newfm_rl2.setOnClickListener(newlessonBeen.get(1));
            holder.newfm_rl3.setOnClickListener(newlessonBeen.get(2));
        }
        else if (o1.get(0) instanceof HomeBean.DataBean.NewfmBean) {
            ArrayList<HomeBean.DataBean.NewfmBean> newfmBeen = (ArrayList<HomeBean.DataBean.NewfmBean>) o;
            holder.newfmAuthor1.setText(newfmBeen.get(0).getSpeak());
            holder.newfmAuthor2.setText(newfmBeen.get(1).getSpeak());
            holder.newfmAuthor3.setText(newfmBeen.get(2).getSpeak());
            holder.newfmContent1.setText(newfmBeen.get(0).getTitle());
            holder.newfmContent2.setText(newfmBeen.get(1).getTitle());
            holder.newfmContent3.setText(newfmBeen.get(2).getTitle());
            Picasso.with(context).load(newfmBeen.get(0).getCover()).into(holder.newfmImg1);
            Picasso.with(context).load(newfmBeen.get(1).getCover()).into(holder.newfmImg2);
            Picasso.with(context).load(newfmBeen.get(2).getCover()).into(holder.newfmImg3);
            holder.newfrm_title.setText("最新FM");
            holder.newfm_more.setText("更多FM");
            holder.home_newfm_rf1.setOnClickListener(newfmBeen.get(0));
            holder.newfm_rl1.setOnClickListener(newfmBeen.get(0));
            holder.newfm_rl2.setOnClickListener(newfmBeen.get(1));
            holder.newfm_rl3.setOnClickListener(newfmBeen.get(2));


        }
        else if (o1.get(0) instanceof HomeBean.DataBean.DiantaiBean) {
            ArrayList<HomeBean.DataBean.DiantaiBean> diantaiBeen = (ArrayList<HomeBean.DataBean.DiantaiBean>) o;
            holder.diantaiTitle1.setText(diantaiBeen.get(0).getTitle());
            holder.diantaiTitle2.setText(diantaiBeen.get(1).getTitle());
            holder.diantaiTitle3.setText(diantaiBeen.get(2).getTitle());
            holder.diantaiTitle4.setText(diantaiBeen.get(3).getTitle());
            Picasso.with(context).load(diantaiBeen.get(0).getCover()).transform(new CircleTransform()).into(holder.diantaiImg1);
            Picasso.with(context).load(diantaiBeen.get(1).getCover()).into(holder.diantaiImg2);
            Picasso.with(context).load(diantaiBeen.get(2).getCover()).into(holder.diantaiImg3);
            Picasso.with(context).load(diantaiBeen.get(3).getCover()).into(holder.diantaiImg4);
            holder.diantai_rl1.setOnClickListener(diantaiBeen.get(0));
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener, Handler.Callback {
        public final LinearLayout home_viewpager_lin;
        public final ImageView home_viewpager_img;
        private ImageView category1;
        private TextView categoryText1;
        private ImageView category2;
        private TextView categoryText2;
        private ImageView category3;
        private TextView categoryText3;
        private ImageView category4;
        private TextView categoryText4;
        private ImageView category5;
        private TextView categoryText5;
        private ImageView category6;
        private TextView categoryText6;
        private ImageView category7;
        private TextView categoryText7;
        private ImageView category8;
        private TextView categoryText8;

        private ViewPager homeViewpager;

        private ImageView newfmImg1;
        private TextView newfmContent1;
        private TextView newfmAuthor1;
        private ImageView newfmImg2;
        private TextView newfmContent2;
        private TextView newfmAuthor2;
        private ImageView newfmImg3;
        private TextView newfmContent3;
        private TextView newfmAuthor3;

        private ImageView hotfrmBg;
        private ImageView hotfrmImg1;
        private TextView hotfrmSmalltitle1;
        private ImageView hotfrmImg2;
        private TextView hotfrmSmalltitle2;
        private ImageView hotfrmImg3;
        private TextView hotfrmSmalltitle3;

        private ImageView diantaiImg1;
        private TextView diantaiTitle1;
        private ImageView diantaiImg2;
        private TextView diantaiTitle2;
        private ImageView diantaiImg3;
        private TextView diantaiTitle3;
        private ImageView diantaiImg4;
        private TextView diantaiTitle4;


        private Handler handler = new Handler(this);
        public TextView newfm_more;
        public TextView newfrm_title;
        public RelativeLayout category_rl1;
        public RelativeLayout category_rl2;
        public RelativeLayout category_rl3;
        public RelativeLayout category_rl4;
        public RelativeLayout category_rl5;
        public RelativeLayout category_rl6;
        public RelativeLayout category_rl7;
        public RelativeLayout category_rl8;
        public final Context mContext1;
        public RelativeLayout home_newfm_rf1;
        public RelativeLayout diantai_rl1;
        public RelativeLayout newfm_rl1;
        public RelativeLayout newfm_rl2;
        public RelativeLayout newfm_rl3;
        public LinearLayout hotfm_lin1;
        public LinearLayout hotfm_lin2;
        public LinearLayout hotfm_lin3;

        public ViewHolder(View itemView,Context context) {
            super(itemView);
            mContext1 = context;
            categoryView(itemView);
            newfmView(itemView);
            homeViewpager = (ViewPager) itemView.findViewById(R.id.home_viewpager);
            home_viewpager_lin = ((LinearLayout) itemView.findViewById(R.id.home_viewpager_lin));
            home_viewpager_img = ((ImageView) itemView.findViewById(R.id.home_viewpager_img));
            hotfmView(itemView);
            diantialView(itemView);

        }

        public void diantialView(View itemView) {
            diantaiImg1 = (ImageView) itemView.findViewById(R.id.diantai_img1);
            diantaiTitle1 = (TextView) itemView.findViewById(R.id.diantai_title1);
            diantaiImg2 = (ImageView) itemView.findViewById(R.id.diantai_img2);
            diantaiTitle2 = (TextView) itemView.findViewById(R.id.diantai_title2);
            diantaiImg3 = (ImageView) itemView.findViewById(R.id.diantai_img3);
            diantaiTitle3 = (TextView) itemView.findViewById(R.id.diantai_title3);
            diantaiImg4 = (ImageView) itemView.findViewById(R.id.diantai_img4);
            diantaiTitle4 = (TextView) itemView.findViewById(R.id.diantai_title4);
            diantai_rl1 = ((RelativeLayout) itemView.findViewById(R.id.diantai_rl1));
        }

        public void hotfmView(View itemView) {
            hotfrmBg = (ImageView) itemView.findViewById(R.id.hotfrm_bg);
            hotfrmImg1 = (ImageView) itemView.findViewById(R.id.hotfrm_img1);
            hotfrmSmalltitle1 = (TextView) itemView.findViewById(R.id.hotfrm_smalltitle1);
            hotfrmImg2 = (ImageView) itemView.findViewById(R.id.hotfrm_img2);
            hotfrmSmalltitle2 = (TextView) itemView.findViewById(R.id.hotfrm_smalltitle2);
            hotfrmImg3 = (ImageView) itemView.findViewById(R.id.hotfrm_img3);
            hotfrmSmalltitle3 = (TextView) itemView.findViewById(R.id.hotfrm_smalltitle3);
            hotfm_lin1 = ((LinearLayout) itemView.findViewById(R.id.hotfm_lin1));
            hotfm_lin2 = ((LinearLayout) itemView.findViewById(R.id.hotfm_lin2));
            hotfm_lin3 = ((LinearLayout) itemView.findViewById(R.id.hotfm_lin3));

        }

        public void newfmView(View itemView) {
            newfmImg1 = (ImageView) itemView.findViewById(R.id.newfm_img1);
            newfmContent1 = (TextView) itemView.findViewById(R.id.newfm_content1);
            newfmAuthor1 = (TextView) itemView.findViewById(R.id.newfm_author1);
            newfmImg2 = (ImageView) itemView.findViewById(R.id.newfm_img2);
            newfmContent2 = (TextView) itemView.findViewById(R.id.newfm_content2);
            newfmAuthor2 = (TextView) itemView.findViewById(R.id.newfm_author2);
            newfmImg3 = (ImageView) itemView.findViewById(R.id.newfm_img3);
            newfmContent3 = (TextView) itemView.findViewById(R.id.newfm_content3);
            newfmAuthor3 = (TextView) itemView.findViewById(R.id.newfm_author3);
            newfm_more = ((TextView) itemView.findViewById(R.id.newfm_more));
            newfrm_title = ((TextView) itemView.findViewById(R.id.newfm_title1));
            home_newfm_rf1 = ((RelativeLayout) itemView.findViewById(R.id.home_newfm_rl1));
            newfm_rl1 = ((RelativeLayout) itemView.findViewById(R.id.newfm_rl1));
            newfm_rl2 = ((RelativeLayout) itemView.findViewById(R.id.newfm_rl2));
            newfm_rl3 = ((RelativeLayout) itemView.findViewById(R.id.newfm_rl3));
        }

        public void categoryView(View itemView) {
            category_rl1 = ((RelativeLayout) itemView.findViewById(R.id.category_rl1));
            category_rl2 = ((RelativeLayout) itemView.findViewById(R.id.category_rl2));
            category_rl3 = ((RelativeLayout) itemView.findViewById(R.id.category_rl3));
            category_rl4 = ((RelativeLayout) itemView.findViewById(R.id.category_rl4));
            category_rl5 = ((RelativeLayout) itemView.findViewById(R.id.category_rl5));
            category_rl6 = ((RelativeLayout) itemView.findViewById(R.id.category_rl6));
            category_rl7 = ((RelativeLayout) itemView.findViewById(R.id.category_rl7));
            category_rl8 = ((RelativeLayout) itemView.findViewById(R.id.category_rl8));

            category1 = (ImageView) itemView.findViewById(R.id.category1);
            categoryText1 = (TextView) itemView.findViewById(R.id.category_text1);
            category2 = (ImageView) itemView.findViewById(R.id.category2);
            categoryText2 = (TextView) itemView.findViewById(R.id.category_text2);
            category3 = (ImageView) itemView.findViewById(R.id.category3);
            categoryText3 = (TextView) itemView.findViewById(R.id.category_text3);
            category4 = (ImageView) itemView.findViewById(R.id.category4);
            categoryText4 = (TextView) itemView.findViewById(R.id.category_text4);
            category5 = (ImageView) itemView.findViewById(R.id.category5);
            categoryText5 = (TextView) itemView.findViewById(R.id.category_text5);
            category6 = (ImageView) itemView.findViewById(R.id.category6);
            categoryText6 = (TextView) itemView.findViewById(R.id.category_text6);
            category7 = (ImageView) itemView.findViewById(R.id.category7);
            categoryText7 = (TextView) itemView.findViewById(R.id.category_text7);
            category8 = (ImageView) itemView.findViewById(R.id.category8);
            categoryText8 = (TextView) itemView.findViewById(R.id.category_text8);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = home_viewpager_lin.getLeft() + (position - 1 + positionOffset) * home_viewpager_img.getWidth();
            value = Math.max(value, home_viewpager_lin.getLeft());
            value = Math.min(value, home_viewpager_lin.getRight() - home_viewpager_img.getWidth());
            ViewCompat.setTranslationX(home_viewpager_img, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = homeViewpager.getAdapter();
            if (position == 0) {
                homeViewpager.setCurrentItem(adapter.getCount() - 2, false);
            }
            else if (position == adapter.getCount() - 1) {
                homeViewpager.setCurrentItem(1, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int arg1 = msg.arg1;
                    if (arg1 > homeViewpager.getAdapter().getCount() - 2) {
                        arg1 = 1;
                    }
                    homeViewpager.setCurrentItem(arg1, arg1 != 1);
                    Message message = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message, 2000);
                    break;
            }
            return true;
        }
    }


}
